package com.cunjin.visitor.service.impl;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.cunjin.base.service.impl.BaseServiceImpl;
import com.cunjin.dormitory.dao.DormitoryDao;
import com.cunjin.dormitory.entity.DormitoryEntity;
import com.cunjin.student.dao.StudentDao;
import com.cunjin.student.entity.StudentEntity;
import com.cunjin.visitor.dao.VisitorDao;
import com.cunjin.visitor.entity.VisitorEntity;
import com.cunjin.visitor.service.VisitorService;


@Service
public class VisitorServiceImpl extends BaseServiceImpl<VisitorEntity> implements VisitorService {

	@Autowired
	private VisitorDao dao;
	@Autowired
	private StudentDao studentDao;
	@Autowired
	private DormitoryDao dormitoryDao;
	
	
	/**
	 * 分页
	 */
	@Transactional(readOnly=true)
	@Override
	public Map<String, Object> queryByPage(int page, int rows, String searchName) {
		//将分页的数据保存在map中
				Map<String, Object> pages=new HashMap<String, Object>();
				
				int before=(page-1)*rows;
				int after=rows;
				pages.put("before", before);
				pages.put("after", after);
				if(null !=searchName){
					pages.put("searchName", "%"+searchName+"%");
				}else{
					pages.put("searchName", null);
				}
				
				Map<String, Object> map = new HashMap<String, Object>();
				try {
					List<VisitorEntity> ls=dao.myqueryByPage(pages);
					map.put("total",dao.findAllByName(pages));
					map.put("rows",ls);
				} catch (Exception e) {
					e.printStackTrace();
				}
				return map;
			}

	/**
	 * 插入
	 */
	@Transactional(readOnly=false)
	@Override
	public Map<String, Object> insert(VisitorEntity entity) {
		
		Map<String, Object> map = new HashMap<String, Object>();
		try {
			//先判断是否重复
			List<VisitorEntity> history = dao.select(entity);
			int size = history.size();
			//说明没有记录
			if(size==0){
				//由名字获得id,保存主键
				Map<String,Object> codition=new HashMap<String,Object>();
				String studentName = entity.getStudentName();
				String dormitoryName = entity.getDormitoryName();
				DormitoryEntity dor = dormitoryDao.selectByName(dormitoryName);
				//拿到宿舍id
				Integer idDor = dor.getId();
				
				//根据宿舍id和studentName去查找学生的id(不可以避免唯一性)
				codition.put("studentName", studentName);
				codition.put("idDor", idDor);
				StudentEntity studentEnt = studentDao.selectByNameAndDormitoryName(codition);
				//拿到学生的id
				Integer stuId = studentEnt.getId();
				entity.setDormitoryId(idDor);
				entity.setStudentId(stuId);
				int successi = dao.insertSelective(entity);
				if(successi==1){
					map.put("code", 200);
					map.put("msg", "插入成功");
					return map;
				}else{
					map.put("code", 201);
					map.put("msg", "插入失败");
					return map;
				}
			}
			
		} catch (Exception e) {
			map.put("code", 500);
			map.put("msg", "插入失败");
			e.printStackTrace();
			return map;
		}
		return map;
	}

	
	@Transactional(readOnly=false)
	@Override
	public Map<String, Object> deleteByids(String[] split) {
		
		Map<String, Object> map = new HashMap<String, Object>();
		try {
			
			int cs=0;
			for (String s : split) {
				int did = Integer.parseInt(s);
				dao.deleteByPrimaryKey(did);
				cs++;
			}
			map.put("code", 200);
			map.put("count", cs);
		} catch (Exception e) {
			map.put("code",500);
			e.printStackTrace();
		}
		return map;
	}
	
	/**
	 * 更新
	 * @param entity
	 * @return
	 */
	@Transactional(readOnly=false)
	@Override
	public Map<String, Object> update(VisitorEntity entity) {
		Map<String, Object> map = new HashMap<String, Object>();
		try {
			
			//当修改了学生和宿舍
			String newstudentName = entity.getStudentName();
			String newDormitoryName = entity.getDormitoryName();
			//拿到学生id和宿舍id
			DormitoryEntity dorentity;
			
			dorentity = dormitoryDao.selectByName(newDormitoryName);
			Integer dorId = dorentity.getId();
			
			Map<String,Object> codition=new HashMap<String,Object>();
			//根据宿舍id和studentName去查找学生的id(不可以避免唯一性)
			codition.put("studentName", newstudentName);
			codition.put("idDor", dorId);
			StudentEntity stuentity = studentDao.selectByNameAndDormitoryName(codition);
			if(null !=stuentity){
				Integer stuId = stuentity.getId();
				//设置最新的学生id和宿舍id
				entity.setStudentId(stuId);
				entity.setDormitoryId(dorId);
				int i = dao.updateByPrimaryKeySelective(entity);
				
				if(1==i){
					map.put("code", 200);
					map.put("msg","更新成功");
					return map;
				}else{
					map.put("code", 500);
					map.put("msg","更新失败");
					return map;
					
				}
				
			}else{
				map.put("code", 201);
				map.put("msg","该宿舍没有这个人");
				return map;
			}
		} catch (Exception e) {
			map.put("code", 500);
			map.put("msg","更新失败");
			e.printStackTrace();
		}
		return map;
	}

}
